

require "TSLib"
init(0)



tab = {
    "03f80000fe1ff00007fcffe0001ffbffc001fff7ff8007ffefff003fffdffe01ffffff000ffffff8007ffffff003fff8fff03fffe1ffffffff03f7fffffc07efffffc00fdfffff001f9ffff8003f0fffc0007e1ffe0000fc07e000000@0$2$458$39$19",
    "01ffffff801fffffffc03fffffffc1ffffffffe7ffffffffcfffffffffdffffffffffffffffffffc00f0007ff003c0007fe00f8000ffc03f800ff7fc7fffffeff8fffffffff1ffffff9fe1fffffe3fc3fffff83f83ffffe03f03ffff801e019ff00$6$579$39$20",
    "03c00000001f000000007c00000001f00000000fc00000003f80000001ffffffffcfffffffff3ffffffffffffffffffffffffffffffffffffffffffffffdfffffdfc0$1$321$38$14",
    "01ffffff800fffffffc07fffffffc1ffffffffc7ffffffffcfffffffffdfffffffffffffffffffff0000003ff00000007fe0000000fbe000000ff7ffff7fffefffffffffdfffffffff9ffffffffe3ffffffff83fffffffe01fffffff0003fff8000$0$583$39$20",
    "4fffc1ff03ffff07ff0ffffc1fff3ffff07ffcffffc1fffbffff07ffeffffc1ffffffff07ffff81e0000ffe0f80001ff83e0000ffe1ffbfffff87fefffffe1ffbfffff87feffffbe0ffbfffef83feffff3e07fbfff80007efff800003bfe00$5$542$38$20",
    "07fc17ffe03ffe6fffe0fffddfffe1ffffbfffe3ffff7ffffffffefffffffffdfffffffffb007ffc01f0003ff003e0007ff007c000ffffffbfffffffff7ffffffffeffffdffffdffff9ffffbfffe3fff37fff83ffc2fffe01ff01fff0@0$8$578$39$19",
    "000001fe0000003ff800001fffe00003ffff80007ffffe000ffffff800ffff87e03fffc01f807ff0007e03fe0001f80ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffcffdffdff00000007e0@00$4$480$38$19",
    "001be007001fffe80fc07fffdc1fe0ffffb83fc7ffff787fcffffef8ffdffffdf1fffffffbe3ffff0017c03ff8000f007fe0001e00ffe0003803ffffff7ffffffffefffffffffdffff9ffffbfffe7ffff7fff87fffeffff07fffdfff803fffbffc0$9$563$39$20",
    "0ff000ffe07fe001ffe1ffc003ffe3ff8007ffefff000ffffffe001ffffffc7c3ffffff8f83fbffc01f0003ff003f0007fe007f001ffffffffffffffffffffefffffffffdfffffffff1ffffffffe3fff3ffff81ffc3fffe00fe01bfe0@0$3$524$39$19",
    "7c00000001f000000007c00000005f0000003f7c00000ffdf00003fff7c003ffffdf03ffffff7f7fffffffffffffffffffffffff3fffffffc0ffffffc003ffffe0000ffff000003ff0000000$7$332$38$16",
    }
index = addTSOcrDictEx(tab)

tab1 = {
    "07ff81fffe3ffff7fffff0003e0001c0000c0000e0001700077ffff3ffff1fffc03ff00000000000000000000007ff81fffe3ffff7fffff0003e0001c0000c0000e0001700077ffff3ffff1fffc$00$292$20$31",
    }
index1 = addTSOcrDictEx(tab1)


build_spacing = 150;  -- x y  通用


build_color = 0x3a944a;
exec_count = 0;  -- 执行计数
graden = 0; -- 分数
tap_left_x = 42;
tap_left_y = 1458;
tap_right_x = 986;
tap_right_y = 1458;
obstacle_color = 0x579b3b; -- 障碍物
obstacle_spacing_y = 100;  -- 障碍物间距
obstacle_spacing_x = 20;
w,h = getScreenSize();
x= -1;
y= -1;
original_color = 0x3a944a; -- 源点
left_original_color =   0x973232 
left_original_x =  544
left_original_y =  1058
right_original_color =    0x973232
right_original_x =   542
right_original_y = 1053


-- left --right
function search_path(x,y,last_path) 
    local lx = x-build_spacing;
    local ly = y-build_spacing;
    local rx = x+build_spacing;
    local ry = y-build_spacing;


    if x < 150 and y < 150 then
        return -1;
    end

    if isColor(lx,ly,build_color) == true and (isColor(lx+obstacle_spacing_x, ly-obstacle_spacing_y, obstacle_color,85) == false) then
        

        -- log("[DATE][LINE][PATH]".."enable_left","test日志")
        return 0;
    end

    if isColor(rx,ry,build_color) == true and (isColor(rx-obstacle_spacing_x, ry-obstacle_spacing_y, obstacle_color,85) == false) then
        -- log("[DATE][LINE][PATH]".."enable_right","test日志")
        return 1;
    end

    -- if enable_left == true and enable_right == true then
    --     log("[DATE][LINE][PATH]".."enable_left_an_right","test日志")
    --     return search_path(lx,ly,0);
    -- elseif enable_left == true then
        
    -- elseif enable_right == true then
        
    -- elseif last_path == 1 then
    --     log("[DATE][LINE][PATH]".."enable_left","test日志")
    --     return 0;
    -- elseif last_path ==0 then
    --     log("[DATE][LINE][PATH]".."enable_right","test日志")
    --     return 1;
    -- else
        return -1;
    -- end

    
end


function loop_exec_script() 



    
    -- 角色方向朝左
if x == -1 or y == -1 then
    start = os.clock()
    x,y = findMultiColorInRegionFuzzy( 0x3a944a, "-23|-88|0xf9e0d0,0|-90|0xb72f1e,18|-90|0xe5c4c4", 90, 414, 978, 662, 1215)
    log("[DATE][LINE][PATH]"..'角色方向朝左搜寻用时'..tostring(os.clock() - start),"speed日志")
end

if x == -1 or y == -1 then
    -- 角色方向朝右
    start = os.clock()
    x,y = findMultiColorInRegionFuzzy( 0x3a944a, "23|-88|0xf9e0d0,0|-90|0xb72f1e,-18|-90|0xe5c4c4", 90, 414, 978, 662, 1215)
    log("[DATE][LINE][PATH]"..'角色方向朝右搜寻用时'..tostring(os.clock() - start),"speed日志")
end


-- 找不到角色直接停止运行
if  x == -1 or y == -1 then
    -- log("[DATE][LINE][PATH]".."未发现角色","test日志")
    return
else
    exec_path = {}; -- 路径 0 left , 1 right, -1 无路可走
    x= 540;
    y=1150;

    start = os.clock()
    while isColor(x,y,original_color) == false do
    end
    log("[DATE][LINE][PATH]"..'源点定位总用时'..tostring(os.clock() - start),"speed日志")

    if graden > 0  and graden < 5000 then
        start = os.clock()
        ret = tsOcrText(index,812, 52, 918, 107,"FDD385 , 032563", 90,90)
        log("[DATE][LINE][PATH]"..'初次搜索积分用时'..tostring(os.clock() - start),"speed日志")
        while graden ~= 5000 and graden ~= tonumber(ret) do
            inner_start = os.clock()
            ret = tsOcrText(index,812, 52, 918, 107,"FDD385 , 032563", 90,90)
            log("[DATE][LINE][PATH]"..'循环搜索积分用时'..tostring(os.clock() - inner_start),"speed日志")
        end
        log("[DATE][LINE][PATH]"..'积分确认搜索总用时'..tostring(os.clock() - start),"speed日志")
    end


    -- current_time = os.date("%Y-%m-%d-%H-%M-%S", os.time());
    -- snapshot(current_time..'----'..exec_count.."执行前记录.png", 0, 0, w-1, h-1);
    -- log("[DATE][LINE][PATH]"..string.format('执行前记录%d次 -- 文件：%s',exec_count, current_time..'---'..exec_count..".png"),"test日志")
    while true do
        exec_res = -1;
        -- toast(string.format('x:%d , y :%d',x ,y));
        -- mSleep(500);
        -- table.insert(exec_path, search_path(x,y,nil));
        search_start = os.clock()
        exec_res = search_path(x,y,nil);
        search_end = os.clock()
        log("[DATE][LINE][PATH]"..'路径单次搜寻用时'..tostring(search_end - search_start),"speed日志")
        -- log("[DATE][LINE][PATH]".."result values: "..exec_res,"test日志")
        if exec_res == 1 then
            -- log("[DATE][LINE][PATH]".."result_1","test日志")
            x = x + build_spacing;
            y = y - build_spacing;
            
        elseif exec_res == 0 then
            -- log("[DATE][LINE][PATH]".."result_0","test日志")
            x = x - build_spacing;
            y = y - build_spacing;
        
        else 
            break
        end;
        table.insert(exec_path, exec_res);
        exec_count=exec_count+1
    end
    log("[DATE][LINE][PATH]"..string.format('执行前记录----%d次 ',exec_count),"speed日志")
    graden = exec_count *5


    start = os.clock()
    for i, v in ipairs(exec_path) do
        if v == 0 then
            tap(tap_left_x,tap_left_y);
        elseif v == 1 then
            tap(tap_right_x,tap_right_y);
        end
    end 
    log("[DATE][LINE][PATH]"..'按钮单击总用时'..tostring(os.clock() - start),"speed日志")
    x= 540;
    y=1150;

     
end


end


start = os.clock()
time_flag = tsOcrText(index1,  292, 47, 348, 116, "FFFFFF , 010101", 90)
log("[DATE][LINE][PATH]"..'初次识别开始界面用时'..tostring(os.clock() - start),"speed日志")
while '00' ~= time_flag do
    inner_start = os.clock()
    time_flag = tsOcrText(index1,  292, 47, 348, 116, "FFFFFF , 010101", 90)
    log("[DATE][LINE][PATH]"..'循环识别开始界面用时'..tostring(os.clock() - inner_start),"speed日志")
end
tailer = os.clock()
log("[DATE][LINE][PATH]"..'识别开始界面用时'..tostring(tailer - start),"speed日志")


start = os.clock()
time_flag = tsOcrText(index1,  292, 47, 348, 116, "FFFFFF , 010101", 90)
log("[DATE][LINE][PATH]"..'初次识别游戏开始界面用时'..tostring(os.clock() - start),"speed日志")
while '00' == time_flag do
    inner_start = os.clock()
    time_flag = tsOcrText(index1,  292, 47, 348, 116, "FFFFFF , 010101", 90)
    log("[DATE][LINE][PATH]"..'循环识别游戏开始界面用时'..tostring(os.clock() - inner_start),"speed日志")
end
tailer = os.clock()
log("[DATE][LINE][PATH]"..'识别游戏开始界面用时'..tostring(tailer - start),"speed日志")


while true do
    if graden >= 5000 then 
        break
    end
    loop_exec_script()
    
end 
